<%@ include file="/common/taglibs.jsp"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><fmt:message key="budget.center.manager.title"/></title>
<script type="text/javascript" language="javascript" src="<c:url value='/My97DatePicker/WdatePicker.js'/>"></script>
<style type="text/css">
li.folded .userEvents {
	display: none;
}
li.expanded .userEvents {
	display: table;
}
li.expanded .expand {
	display: none;
}
li.folded .fold {
	display: none;
}
</style>
</head>
<body>

	<c:if test="${empty bcList }">
		<fmt:message key="budget.center.manager.empty"></fmt:message>
	</c:if>

	<c:if test="${not empty bcList }">

	<form:form method="get" commandName="search">
		<fmt:message key="event.info.starttime" />:
		&nbsp;<input id="_startTime" class="Wdate" name="startTime" type="text"
			 value="${startTime }" onfocus="WdatePicker()"/>&nbsp; 
		<fmt:message key="event.info.endtime" />:
		&nbsp;<input id="_endTime" class="Wdate" name="endTime" type="text"
			 value="${endTime }" onfocus="WdatePicker()"/>
		<fmt:message key="event.info.budgetCenter"></fmt:message>
		<select name="bcId">
			<c:forEach items="${bcList }" var="bc">
				<option value="${bc.id }" <c:if test="${bcId == bc.id }">selected='true'</c:if>>${bc.bcName }</option>
			</c:forEach>
		</select>
		
		<input type="submit" value="<fmt:message key='button.query' />"/> 
	</form:form>

	<ul style="list-style: none;">
	<c:forEach items="${userNames }" var="userEntry">
		<li id="p${userEntry.key }" class="folded">
			<p>
				${userEntry.value.email } (${userEntry.value.fullName })  (${hours[userEntry.key] } <fmt:message key="hours"></fmt:message>) 
				<fmt:message key="bc.manager.review.sum">
					<fmt:param><span id="${userEntry.key }_reject">${reject[userEntry.key] }</span></fmt:param>
					<fmt:param><span id="${userEntry.key }_passed">${passed[userEntry.key] }</span></fmt:param>
					<fmt:param><span id="${userEntry.key }_pending">${pending[userEntry.key] }</span></fmt:param>
				</fmt:message>
				<button class="expand" onclick="expand('p${userEntry.key }')"><fmt:message key="expand"/></button>
				<button class="fold" onclick="fold('p${userEntry.key }')"><fmt:message key="fold"/></button>
				<button onclick="passUser('${userEntry.key}', '${bcId }', '${startTime }', '${endTime }');"><fmt:message key="bc.manager.passed.all"/></button>
			</p>
			<display:table name="${userDetails[userEntry.key]}" id="event" class="table userEvents" requestURI="/event/bcManager.html">
				<display:column titleKey="event.info.date" group="1" sortProperty="startDate" property="startDate" sortable="true"/>
				<display:column titleKey="event.info.starttime" property="startTime" sortable="true"/>
				<display:column titleKey="event.info.endtime" property="endTime" sortable="true"/>
				<display:column titleKey="event.info.name" property="eventName"/>
				<display:column titleKey="event.info.duration" property="duration" sortable="true"/>
				<display:column titleKey="bc.manager.review">
					<span id="review${event.id }" style="display: block; width: 100px;">
					<c:if test="${event.isReviewed }">
						<c:choose>
							<c:when test="${event.isPassed }">
								<fmt:message key="bc.manager.review.passed"></fmt:message>
							</c:when>
							<c:otherwise>
								<fmt:message key="bc.manager.review.reject"></fmt:message>
							</c:otherwise>
						</c:choose>
					</c:if>
					</span>
					<input type="text" id="remark_${event.id }" value="${event.reviewRemarks }">
					<button onclick="passUserEvent('${event.id}')"><fmt:message key="bc.manager.passed"/></button>
					<button onclick="rejectUserEvent('${event.id}')"><fmt:message key="bc.manager.reject"/></button>
				</display:column>
			</display:table>
		</li>
	</c:forEach>
	</ul>
	
	</c:if>
	<script type="text/javascript">
	
	var biscuits = new Cookies();
	
	function expand(id) {
		$(id).className="expanded";
		var explist = biscuits.get("expanded");
		if (explist) {
			biscuits.set("expanded", explist + "," + id);
		} else {
			biscuits.set("expanded", id);
		}
	}
	function fold(id) {
		$(id).className="folded";
		var explist = biscuits.get("expanded");
		if (explist) {
			explist = explist.split(",");
			var newexplist = "";
			for (i = 0; i < explist.length; i++) {
				if (explist[i] == id) {
					break;
				} else {
					newexplist += (id + ",");
				}
			}
			biscuits.set("expanded", newexplist.substr(0, newexplist.length-1));
		}
	}
	
	window.onload = function () {
		var explist = biscuits.get("expanded").split(",");
		for (i = 0; i < explist.length; i++) {
			$(explist[i]).className="expanded";
		}
	}
	
	function passUser(username, bcId, startTime, endTime) {
		new Ajax.Request('bcMgrReview.html?ajax=true&username='+username+"&bcId="+bcId+"&startTime="+startTime+"&endTime="+endTime+"&t="+new Date().getTime(),
		{
			method:"get",
			onSuccess: function (transport) {
				var response = transport.responseText || "no response text";
			},
			onFailure: function () {
				alert("Something went wrong.");
			}
		});
	}
	function passUserEvent(eventId) {
		var remark = $("remark_"+eventId).value;
		new Ajax.Request('bcMgrReview.html?ajax=true&passed=true&eventId='+eventId+"&remark="+remark+"&t="+new Date().getTime, {
			method: "get",
			onSuccess: function (transport) {
				var response = transport.responseText || "no response text";
				if ("no response text" != response) {
					var rev = $("review" + response);
					rev.update('<fmt:message key="bc.manager.review.passed"></fmt:message>');
				}
			},
			onFailure: function () {
				alert("Something went wrong.");
			}
		});
	}
	function rejectUserEvent(eventId) {
		var remark = $("remark_"+eventId).value;
		new Ajax.Request('bcMgrReview.html?ajax=true&passed=false&eventId='+eventId+"&remark="+remark+"&t="+new Date().getTime, {
			method: "get",
			onSuccess: function (transport) {
				var response = transport.responseText || "no response text";
				if ("no response text" != response) {
					var rev = $("review" + response);
					rev.update('<fmt:message key="bc.manager.review.reject"></fmt:message>');
				}
			},
			onFailure: function () {
				alert("Something went wrong.");
			}
		});
	}
	</script>
</body>
</html>